package com.zzxz.practicaltrainingsys.controller;

import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Datafile;
import com.zzxz.practicaltrainingsys.service.DatafileService;
import com.zzxz.practicaltrainingsys.util.FileUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/datafile")
@CrossOrigin
public class DatafileController {

    @Autowired
    private DatafileService datafileService;

    //获取所有文件类型
    @GetMapping("/types")
    public List<String> getDatafileTypes() {
        return datafileService.getDatafileTypes();
    }

    //查询文件资料列表
    //http://localhost:8080/datafile/list
    @GetMapping("/list")
    public List<Datafile> getDatafileList() {
        List<Datafile> datafiletInfo = datafileService.getDatafileInfo();
        return datafiletInfo;
    }
    //资料信息表插入
    @PostMapping("/insert")
    public int insertDatafile(@RequestBody Datafile datafile) {

        return datafileService.insertDatafile(datafile);
    }
    //资料信息表删除
    @DeleteMapping("/delete")
    public int deleteDatafile(@RequestParam Integer data_id) {
        return datafileService.deleteDatafileById(data_id);
    }

    //用来查询所有文件信息（联合项目表和老师表）
    //http://localhost:8080/datafile/all
    @GetMapping("/all")
    public List<Map<String, Object>> getAllDataFiles() {
        return datafileService.getAllDataFiles();
    }

    @ApiOperation("查找文件资料信息（分页查询）")
    @GetMapping("/pages")
    public PageInfo<Map<String, Object>> getPagesDatafielList(Integer pageNum, Integer pageSize) {
        PageInfo<Map<String, Object>> pageInfo = datafileService.findDatafilePages(pageNum,pageSize);
        return pageInfo;
    }

    @ApiOperation("图片/文件上传操作")
    @PostMapping("/upload")
    public String getPicFileName(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new IllegalArgumentException("File cannot be null or empty");
        }

        String originalFileName = file.getOriginalFilename();
        String filePath = FileUtil.getUpLoadFilePath();

        try {
            // 生成唯一文件名
            String uniqueFileName = FileUtil.generateUniqueFileName(originalFileName);

            // 上传文件，传入生成的唯一文件名
            FileUtil.uploadFile(file.getBytes(), filePath, uniqueFileName);

            // 打印生成的文件名，用于调试
            System.out.println("Generated file name: " + uniqueFileName);
            System.out.println("File saved to: " + filePath + "/" + uniqueFileName);

            // 返回完整的文件路径
            return "images/upload/" + uniqueFileName;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to upload file: " + e.getMessage(), e);
        }
    }
    //实训圈插入文件
    @PostMapping("/circleInsertDatafile")
    public int circleInsertDatafile(@RequestBody Datafile datafile) {

        return datafileService.circleInsertDatafile(datafile);
    }

    //根据学生id查询文件
    @GetMapping("/bystudent/{student_id}")
    public List<Map<String,Object>>
    getDataFileByStudent(@PathVariable Integer student_id){
        return datafileService.getDataFileByStudent(student_id);
    }

    // 根据 teacher_id 和 project_id 查询文件
    @GetMapping("/allByTeacherAndProject")
    public List<Map<String, Object>> getDataFilesByTeacherAndProject(
            @RequestParam(value = "teacher_id", required = false) Integer teacher_id,
            @RequestParam(value = "project_id", required = false) Integer project_id) {
        return datafileService.getDataFilesByTeacherAndProject(teacher_id, project_id);
    }

    //根据project_id文件和对应id下老师姓名
    @GetMapping("/byProjectid/{project_id}")
    public List<Map<String, Object>> getDataFilesByProjectId(@PathVariable Integer project_id){
        return datafileService.getDataFilesByProject(project_id);
    }
}
